<template>
  <div>
    <div class="add-btn-container">
      <button class="add-btn" @click="addExample">新增</button>
    </div>
    <table>
      <thead>
        <tr>
          <th>序号</th>
          <th>示例</th>
          <th>URL</th>
          <th>描述</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(example, index) in paginatedExamples" :key="index">
          <td>{{ index + 1 }}</td>
          <td>{{ example.name }}</td>
          <td>
            <a :href="example.url" target="_blank">{{ example.url }}</a>
          </td>
          <td>{{ example.description }}</td>
          <td>
            <button class="view" @click="viewExample(index)">查看</button>
            <button class="edit" @click="editExample(index)">编辑</button>
            <button class="delete" @click="deleteExample(index)">删除</button>
          </td>
        </tr>
      </tbody>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>{{ currentPage }} / {{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">
        下一页
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: "ExamplesTable",
  data() {
    return {
      examples: [],
      currentPage: 1,
      itemsPerPage: 5,
    };
  },
  methods: {
    getExamples() {
      console.log("getExamples");
      for (let i = 0; i < 100; i++) {
        this.examples.push({
          name: `Example ${i + 1}`,
          url: `httphis is example ${i + 1}`,
          description:`This is example ${i + 1}`,
        });
      }
    },
    prevPage() {
      console.log("prevPage");
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      console.log("nextPage");
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
    editExample(index) {
      // 编辑示例
      console.log(`Editing example ${index + 1}`);
    },
    viewExample(index) {
      // 查看示例
      console.log(`Viewing example ${index + 1}`);
    },
    deleteExample(index) {
      // 删除示例
      this.examples.splice(index, 1);
    },
    addExample() {
      // 添加示例
      console.log("Adding example");
    },
  },
  computed: {
    paginatedExamples() {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.examples.slice(startIndex, endIndex);
    },
    totalPages() {
      return Math.ceil(this.examples.length / this.itemsPerPage);
    },
  },
};
</script>

<style scoped>
@import url("@/assets/css/examples.css");
</style>
